/**
 * 罚没证号生成
 * @param params
 */
function cardGenerated() {
    const self = this;
    let regionCode = self.$$("tree_single_select_6rwcpk").selectedData[0].data.columns.CODE;
    let lawEnforcementCode = self.$$('single_select_867a32').cValue;
    let randomNum = Math.floor(Math.random() * 10000).toString().padStart(4, "0");

    getCardNumber(regionCode, lawEnforcementCode).then(result => {
        console.log(result);
    });
    // region_code manage_unit_code   查询 current_sequence


    let cardNo = `${regionCode}${lawEnforcementCode}${randomNum}`;
    self.$$('text_input_fr4lav').cValue = cardNo;
}


function getCardNumber(regionCode, lawEnforcementCode) {
    return new Promise(function (resolve, reject) {
        // let law_confiscated_card_number_index = 'ds_tbl:t_50c0a039d1274a2b83d2ba0029107055';
        api.EXEC_FORMULA(`ENTITY_SELECT_ONE('ds_tbl:t_50c0a039d1274a2b83d2ba0029107055', 'current_sequence', QUERY_AND(QUERY_EQ('region_code','${regionCode}'), QUERY_EQ('manage_unit_code','${lawEnforcementCode}')))`).then(function (res) {
            debugger
            let currentSequence = 0;
            if (res === null || res === '') {
                currentSequence = 1;
                nsertLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence)
            } else {
                currentSequence = res.result++  ;
            }
            resolve(currentSequence);
        });
    })
}


function insertLawConfiscatedCardInfo(regionCode, lawEnforcementCode, currentSequence) {
    const insertApprovalSql = "ds_tbl:t_50c0a039d1274a2b83d2ba0029107055',['region_code','" + regionCode + "','manage_unit_code','" + lawEnforcementCode + "','current_sequence','" + currentSequence + "','create_time',NOW()])";
    api.EXEC_FORMULA(insertApprovalSql).then(res => {

    })
}
